package com.njym.java.security.app.rest;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njym.java.security.app.biz.SupportBussinessBiz;
import com.njym.java.security.app.biz.TravelBussinessRelationBiz;
import com.njym.java.security.app.biz.TravelDistanceBiz;
import com.njym.java.security.app.entity.TravelBussinessRelationVo;
import com.njym.java.security.app.entity.TravelDistance;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("travelDistance")
public class TravelDistanceController extends BaseController<TravelDistanceBiz,TravelDistance> {
    @Autowired
    private SupportBussinessBiz supportBussinessBiz;
    @Autowired
    private TravelBussinessRelationBiz travelBussinessRelationBiz;

    /**
     * 获取行程列表,按时间倒序
     * @return
     */
    @RequestMapping(value = "/getLists", method = RequestMethod.GET)
    @ResponseBody
    public Object getLists(@RequestParam(defaultValue = "10") int limit,
                                                @RequestParam(defaultValue = "1") int page){
        Map<String, Object> data = new HashMap<>();
        Page<TravelDistance> result = PageHelper.startPage(page, limit);
        baseBiz.getLists();
        List<Object> list = new ArrayList<>();
        if ( result.getResult().size() > 0 ) {
            result.getResult().forEach( item -> {
                Map<String, Object> map = new HashMap<>();
                List<TravelBussinessRelationVo> bussinesses = travelBussinessRelationBiz.selectSupportBuss(item.getId());
                map.put("bussinesses", bussinesses);
                map.put("distance",item);
                list.add(map);
            });
            data.put("total", result.getPages());
            data.put("rows", list);
            return new ObjectRestResponse<>().rel(true).data(data);
        }else {
            return new ObjectRestResponse<>().rel(false);
        }

    }

    @RequestMapping(value = "/getCanSupported", method = RequestMethod.GET)
    @ResponseBody
    public Object getCanSupported(String place){
        Map<String, Object> data = new HashMap<>(1);
        List<TravelDistance> list = baseBiz.getCanSupported(place);
        data.put("list", list);
        return new ObjectRestResponse<>().data(data);
    }

}